package com.wxy.config;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;

import com.wxy.dao.userMapper;
import com.wxy.domain.RedisUser;
import com.wxy.domain.user;

import cn.hutool.json.JSONUtil;

@Configuration
public class UserDetailsConfig2 implements UserDetailsService {
	@Autowired
	private userMapper userMapper;
	@Override
	public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
		// TODO 自动生成的方法存根
		user user=new user();
		user=userMapper.findUserByUsername(username);
//		String string=stringRedisTemplate.opsForValue().get("user:1");
//		RedisUser redisUser=JSONUtil.toBean(string,RedisUser.class);
//		user=redisUser.getUser();
		if(user==null)
			throw new UsernameNotFoundException("用户名或密码错误");
		return User
				.withUsername(user.getUsername())
				.password(user.getPassword())
				.roles(user.getRole())
				.build();
	}
}
